package page;

import common.Object;
import common.RegisterUser;
import common.User;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.edge.EdgeDriver;

public class RegisterPage {
    EdgeDriver edgeDriver = Object.getEdgeDriver();
    private final String Registerurl = "http://82.156.186.83:8080/sign-up.html";

    public void openRegisterPage() {
        edgeDriver.get(Registerurl);
        WebElement RegisterTitle = edgeDriver.findElement(By.cssSelector("#signUpForm > div > h2"));

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(RegisterTitle.getText());
        System.out.println("注册页面查找:" + "用户注册".equals(RegisterTitle.getText()));
    }

    /**
     * 1.打开注册界面
     * 2.输入用户名，昵称，密码，确认密码
     * 3.点击注册
     * 4.获取title，跳转到用户登录
     */
    public void register(RegisterUser user) throws InterruptedException {
        openRegisterPage();
        WebElement nameInput = edgeDriver.findElement(By.cssSelector("#username"));
        Thread.sleep(1000);
        nameInput.sendKeys(user.getName());

        WebElement nicknameInput = edgeDriver.findElement(By.cssSelector("#nickname"));
        Thread.sleep(1000);
        nicknameInput.sendKeys(user.getNickName());
        WebElement passwordInput = edgeDriver.findElement(By.cssSelector("#password"));
        Thread.sleep(1000);
        passwordInput.sendKeys(user.getPassword());
        WebElement passwordRepeatInput = edgeDriver.findElement(By.cssSelector("#passwordRepeat"));
        Thread.sleep(1000);
        passwordRepeatInput.sendKeys(user.getPasswordRepeat());

        WebElement registerButton = edgeDriver.findElement(By.cssSelector("#submit"));
        registerButton.click();
        Thread.sleep(2000);

        String title = edgeDriver.getTitle();
        //判断写的不对
        if ("论坛 - 用户登录".equals(title)) {
            System.out.println("注册成功");
        } else if ("论坛 - 用户注册".equals(title)) {
            System.out.println("注册失败");
        }

    }

    public void goToLoginPage() throws InterruptedException {
        openRegisterPage();
        WebElement loginButton = edgeDriver.findElement(By.cssSelector("body > div > div > div > div:nth-child(1) > div > div.text-center.text-muted.mt-3 > a"));
        Thread.sleep(2000);
        loginButton.click();
        //查找注册页面元素
        WebElement registerElement = edgeDriver.findElement(By.cssSelector("#signUpForm > div > h2"));
        Thread.sleep(1000);
        System.out.println(registerElement.getText());
        System.out.println("用户登录点击：" + "用户注册".equals(registerElement.getText()));
    }

    public void close() {
        edgeDriver.close();
    }
}
